//
//  MyAdoptedViewController.m
//  PigMarket
//
//  Created by mac on 2019/5/6.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "MyAdoptedViewController.h"
#import "AdoptedPigsViewController.h"
#import "FreeAdoptedPigsViewController.h"
#import "OrderAdoptedPigsViewController.h"
#import "TransferManagerViewController.h"

@interface MyAdoptedViewController ()<XXPageTabViewDelegate>
/**
 bgImg
 */
@property (nonatomic , strong) UIImageView *bgImg;
/**
 pageTableView
 */
@property (nonatomic , strong) XXPageTabView *pageTabView;
/**
 饲养仔猪
 */
@property (nonatomic , strong) AdoptedPigsViewController *adoptedVC;
/**
 免费试养
 */
@property (nonatomic , strong) FreeAdoptedPigsViewController *freeVC;
/**
 99预定
 */
@property (nonatomic , strong) OrderAdoptedPigsViewController *orderVC;
/**
 去养猪Btn
 */
@property (nonatomic , strong) UIButton *toAdoptBtn;
@end

@implementation MyAdoptedViewController


#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的领养";
    [self initNav];
    [self setUpView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.toAdoptBtn removeFromSuperview];

}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

#pragma mark - init method
- (void)initNav {
   // [[NavigationBarManager sharedInstance] setupNavigationBarClear:self.navigationController];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"adopt_adoptdetail_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backTapped)];
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 60, 44);
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn setTitle:@"转让管理" forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [rightBtn addTarget:self action:@selector(transferManagerTapped) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self addChildViewController:self.adoptedVC];
    [self addChildViewController:self.freeVC];
    [self addChildViewController:self.orderVC];
    
    [self.view addSubview:self.bgImg];
    [self.bgImg bringSubviewToFront:self.view];
    [self.view addSubview:self.pageTabView];

    [[UIApplication sharedApplication].keyWindow addSubview:self.toAdoptBtn];
}

- (void) initLayout {
    [self.bgImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(-TopBarHeight);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(197);
    }];
    [self.pageTabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    [self.toAdoptBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.bottom.mas_equalTo(-120);
        make.size.mas_equalTo(CGSizeMake(71, 91));
    }];
}

- (void) initAction {
    
}

- (void)initData {
    
}

#pragma mark - delegate
- (void)scrollToLast:(id)sender {
    [self.pageTabView setSelectedTabIndexWithAnimation:self.pageTabView.selectedTabIndex-1];
}

- (void)scrollToNext:(id)sender {
    [self.pageTabView setSelectedTabIndexWithAnimation:self.pageTabView.selectedTabIndex+1];
}
#pragma mark - target action
- (void)backTapped {

    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)transferManagerTapped {

    [self.navigationController pushViewController:[[TransferManagerViewController alloc] init] animated:YES];
}
#pragma mark - private method

#pragma mark - getter and setter
- (UIImageView *)bgImg {
    if (!_bgImg) {
        _bgImg = [[UIImageView alloc] init];
        _bgImg.image = [UIImage imageNamed:@"adopted_headerbg"];
    }
    return _bgImg;
}
- (XXPageTabView *)pageTabView {
    if (!_pageTabView) {
        
        _pageTabView = [[XXPageTabView alloc] initWithChildControllers:self.childViewControllers childTitles:@[@"饲养仔猪",@"免费试养",@"99元预定"]];
        _pageTabView.delegate = self;
        _pageTabView.indicatorStyle = XXPageTabIndicatorStyleFollowText;//跟随文本变化长度
        _pageTabView.titleStyle = XXPageTabTitleStyleGradient;//渐变
        _pageTabView.selectedTabIndex = 0;
        _pageTabView.separatorColor = [UIColor clearColor];
        _pageTabView.tabItemFont = [UIFont systemFontOfSize:14];
        _pageTabView.indicatorHeight = 0;
        _pageTabView.unSelectedColor = [UIColor colorWithHexString:@"#FFFFFF"];
        _pageTabView.selectedColor = [UIColor colorWithHexString:@"#FFFFFF"];
        _pageTabView.tabBackgroundColor = [UIColor clearColor];
        _pageTabView.bodyBackgroundColor = [UIColor clearColor];
    }
    return _pageTabView;
}
- (AdoptedPigsViewController *)adoptedVC {
    if (!_adoptedVC) {
        _adoptedVC = [[AdoptedPigsViewController alloc] init];
    }
    return _adoptedVC;
}
- (FreeAdoptedPigsViewController *)freeVC {
    if (!_freeVC) {
        _freeVC = [[FreeAdoptedPigsViewController alloc] init];
    }
    return _freeVC;
}
- (OrderAdoptedPigsViewController *)orderVC {
    if (!_orderVC) {
        _orderVC = [[OrderAdoptedPigsViewController alloc] init];
    }
    return _orderVC;
}
- (UIButton *)toAdoptBtn {
    if (!_toAdoptBtn) {
        _toAdoptBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_toAdoptBtn setBackgroundImage:[UIImage imageNamed:@"adopted_toadopt"] forState:UIControlStateNormal];
    }
    return _toAdoptBtn;
}

@end
